package eu.telecom_bretagne.CESI.data.dao;

import java.util.List;

import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import eu.telecom_bretagne.CESI.data.model.Document;

/**
 * Session Bean implementation class DocumentDAO
 */
@Stateless
@LocalBean
public class DocumentDAO implements DAO<Document>{

    @PersistenceContext
	EntityManager entityManager;
	
	
    /**
     * Default constructor. 
     */
    public DocumentDAO() {
        
    }

	@Override
	public Document create(Document entity) {
		
		entityManager.persist(entity);
		return entity;
	}

	@Override
	public Document findById(int id) {
		
		return entityManager.find(Document.class, id);
	}

	@Override
	public Document update(Document entity) {
		
		return entity; //no-op.
	}

	@Override
	public void delete(Document entity) {
		
		entityManager.remove(entity);
	}

	// méthode pour requete SQL afin de lire tous les documents
	public List<Document> findAll() {
		
		Query query = entityManager.createQuery("Select document from Document document");
		
		return (List<Document>) query.getResultList() ;
	}

}
